package com.example.databaseproject;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import java.util.prefs.PreferencesFactory;

public class MainActivity extends AppCompatActivity {

    private EditText account,password;
    private CheckBox checkBox;
    private SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        account = findViewById(R.id.account);
        password = findViewById(R.id.password);
        checkBox = findViewById(R.id.checkBox);
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        boolean isRemember = sharedPreferences.getBoolean("isRemember",false);
        if (isRemember){
            account.setText(sharedPreferences.getString("account",""));
            password.setText(sharedPreferences.getString("password",""));
            checkBox.setChecked(sharedPreferences.getBoolean("isRemember",false));
        }

    }

    public void loginSofter(View view){
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if (checkBox.isChecked()){
            String accountName = account.getText().toString();
            String passwordContext = password.getText().toString();
            editor.putString("account",accountName);
            editor.putString("password",passwordContext);
            editor.putBoolean("isRemember",true);
        }else{
            editor.clear();
        }
        editor.apply();
        Intent intent = new Intent(MainActivity.this,DataBase.class);
        startActivity(intent);
    }
}